package com.yztc.orangeentertainment.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.yztc.orangeentertainment.R;
import com.yztc.orangeentertainment.base.OnItemClickListener;
import com.yztc.orangeentertainment.bean.LabListBean.ListBean;
import com.yztc.orangeentertainment.utils.CircleImageView;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by My on 2016/10/7.
 */
public class LabListAdapter extends RecyclerView.Adapter<LabListAdapter.MyViewHolder> {

    private Context context;

    private List<ListBean> list;
    private OnItemClickListener listener;
    private boolean flag;


    public void setList(List<ListBean> list) {
        this.list = list;
    }

    public void setListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public LabListAdapter(Context context) {
        this.context = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_lablist, null));
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        holder.ivLablist.setImageURI(list.get(position).getPic());
        holder.ivLablist.setColorFilter(0x40000000);
        holder.tvLablistName.setText(list.get(position).getName());
        holder.labAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!flag){
                    flag=true;
                    holder.labAdd.setImageResource(R.mipmap.ic_judge_right_green);

                    Toast.makeText(context,"添加订阅",Toast.LENGTH_SHORT).show();
                }else {
                    flag=false;
                    Toast.makeText(context,"取消订阅",Toast.LENGTH_SHORT).show();
                    holder.labAdd.setImageResource(R.mipmap.add);
                }
            }
        });
        if (listener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    listener.onItemClick(holder.itemView, pos);
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int pos = holder.getLayoutPosition();
                    listener.onItemLongClick(holder.itemView, pos);
                    return false;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        @InjectView(R.id.iv_lablist)
        SimpleDraweeView ivLablist;
        @InjectView(R.id.lab_add)
        CircleImageView labAdd;
        @InjectView(R.id.tv_lablist_name)
        TextView tvLablistName;
        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.inject(this, itemView);
        }
    }

}
